//
//  LoginCorrelationHelper.m
//  YunBroker
//
//  Created by uplooking on 2017/2/21.
//  Copyright © 2017年 RenLong. All rights reserved.
//

#import "LoginCorrelationHelper.h"

@implementation LoginCorrelationHelper

+ (LoginCorrelationHelper *)shareInstance{
    
    static LoginCorrelationHelper *instance = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        
        instance = [[LoginCorrelationHelper alloc] init];
        
    });
    
    return instance;
}

+ (BOOL)loginCorrelationHelperIsLogin{
    
    NSString * token = [UserCenterHelper shareInstance].token;
    
    BOOL isLogin = token.length;
    
    if (!isLogin) {

        LoginViewController *vc=[[LoginViewController alloc]init];
        vc.loginFinish=^{

            if ([self shareInstance].loginFinishBlock) {

                [self shareInstance].loginFinishBlock();
            }

        };

        BaseNavigationViewController *nv = [[BaseNavigationViewController alloc] initWithRootViewController:vc];

        [ApplicationDelegate.window.rootViewController presentViewController:nv animated:YES completion:nil];

    }

    return isLogin;
    
}

+ (BOOL)loginCorrelationHelperLoginOverTime:(NSString *)code{
    
    if ([code isKindOfClass:[NSDictionary class]]) {
        
        return NO;
    }
    
    BOOL isLogin = [code integerValue] == 403;
    
    if (isLogin) {
        
        LoginViewController *vc=[[LoginViewController alloc]init];
        
        BaseNavigationViewController *nv = [[BaseNavigationViewController alloc] initWithRootViewController:vc];
        
        if ([ApplicationDelegate.window.rootViewController isMemberOfClass:[BaseNavigationViewController class]]) {
            
            [(BaseNavigationViewController *)ApplicationDelegate.window.rootViewController  presentViewController:nv animated:YES completion:nil];
            
            [vc setLoginFinish:^{
                
                [(BaseNavigationViewController *)ApplicationDelegate.window.rootViewController popToRootViewControllerAnimated:NO];
            }];
        }
        
    }
    
    return  isLogin;
    
}
@end
